package com.edais.util;


import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;

public class SignUtils {

	private static final String ALGORITHM = "RSA";

	private static final String SIGN_ALGORITHMS = "SHA1WithRSA";

	private static final String DEFAULT_CHARSET = "UTF-8";

	public static String sign(String content, String privateKey) {
		try {
			PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(
					Base64.decode(privateKey));
			KeyFactory keyf = KeyFactory.getInstance(ALGORITHM);
			PrivateKey priKey = keyf.generatePrivate(priPKCS8);

			java.security.Signature signature = java.security.Signature
					.getInstance(SIGN_ALGORITHMS);

			signature.initSign(priKey);
			signature.update(content.getBytes(DEFAULT_CHARSET));

			byte[] signed = signature.sign();

			return Base64.encode(signed);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	
	public static void main(String[] args) {
		String partner="partner=\"2088801034350172\"&seller_id=\"xlhotelzhkj@126.com\"&out_trade_no=\"0309093626-1684\"&subject=\"测试的商品\"&body=\"该测试商品的详细描述\"&total_fee=\"0.01\"&notify_url=\"http://notify.msp.hk/notify.htm\"&service=\"mobile.securitypay.pay\"&payment_type=\"1\"&_input_charset=\"utf-8\"&it_b_pay=\"30m\"&return_url=\"m.alipay.com";
		String rsa = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0QXzX9vd8IjigF39HZMXHCX9AJmdqI1YuyeTcAw2sooKYXiSCkUF9zgH63xewI/cS+4/huNxqCw2I2m7AYikdLlYgf1q/QIBiO11NIMdty6GfPgdJRcmUW3iZKxbkL1zC3luKlUYTcQsAWqeb5Og9LymJa3Q5m8qo6kxaGse3B1HoRrkhP47iaaD3aVDKyWtXE9q4wj8gA/wzog/GGpALxs+xm5MUX5OJ1k/gR9AcNUihaP2upp4I6DQMUGsDRr+tT2Axj4pDPGLFYS2GAkRxRuChuoR9C9Z3tag4Yg50gLHvDasBf68kEiwYbArfBQ3wHoxhjZqaKbPdH5rYbdITwIDAQAB";
		sign(partner,rsa);
	}

}
